<template>
  <a-card :bordered="false" class="card-area">
    <div :class="advanced ? 'search' : null">
      <!-- 搜索区域 -->
      <a-form layout="horizontal">
        <div :class="advanced ? null: 'fold'">
          <a-row>
            <a-col :md="12" :sm="24">
              <a-form-item
                label="法人行"
                :labelCol="{span: 5}"
                :wrapperCol="{span: 18, offset: 1}">
                <a-select
                          mode="default"
                          :allowClear="true"
                          style="width: 100%"
                          @change="handleInstChange"
                          v-model="queryParams.instId">
                  <a-select-option v-for="r in instIdData" :key="r.label">{{r.label}}-{{r.value}}</a-select-option>
                </a-select>
<!--                <a-input v-if="!this.user.admin" v-model="queryParams.instId" :value="this.user.instId"
                         readonly="readonly"/>-->
              </a-form-item>
            </a-col>
            <a-col :md="12" :sm="24" v-show="queryParams.instId!=''">
              <a-form-item
                label="终端品牌代码"
                :labelCol="{span: 5}"
                :wrapperCol="{span: 18, offset: 1}">
                <a-input placeholder="设备编号 , 终端型号 , 终端厂商..." v-model="queryParams.searchParam"/>
              </a-form-item>
            </a-col>
          </a-row>
        </div>
        <div style="text-align: center;margin-bottom: 20px " v-show="queryParams.instId!=''">
          <a-button type="primary" @click="search">查询</a-button>
          <a-button style="margin-left: 8px" @click="reset">重置</a-button>
        </div>
      </a-form>
    </div>
    <div v-show="queryParams.instId!=''">

      <!-- 表格区域 -->
      <a-table ref="TableInfo"
               :columns="columns"
               :rowKey="record => record.id"
               :dataSource="dataSource"
               :pagination="pagination"
               :loading="loading"
               :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
               :scroll="{ x: 2200 }"
               @change="handleTableChange">
        <template slot="email" slot-scope="text, record">
          <a-popover placement="topLeft">
            <template slot="content">
              <div>{{text}}</div>
            </template>
            <p style="width: 150px;margin-bottom: 0">{{text}}</p>
          </a-popover>
        </template>
        <template slot="brandOperation" slot-scope="text, record">
          <a-icon v-hasPermission="['deviceAction:edit']" type="setting" theme="twoTone" twoToneColor="#4a9ff5"
                  @click="editDeviceAction(record)" title="修改"></a-icon>&nbsp;
          <a-icon v-hasPermission="['deviceAction:view']" type="eye" theme="twoTone" twoToneColor="#42b983"
                  @click="viewDeviceAction(record)" title="查看"></a-icon>
          <a-badge v-hasNoPermission="['deviceAction:update','deviceAction:view']" userStatus="warning" text="无权限"></a-badge>
        </template>

      </a-table>
      <!-- 品牌信息查看 -->
      <device-action-info
        ref="deviceActionInfo"
        @close="handleDeviceActionInfoClose"
        :deviceActionInfoVisiable="deviceActionInfo.visiable"
        :deviceActionInfoData="deviceActionInfo.data">
      </device-action-info>
      <!-- 修改品牌 -->
      <device-action-edit
        ref="deviceActionEdit"
        @close="handleDeviceActionEditClose"
        @success="handleDeviceActionEditSuccess"
        :deviceActionEditVisiable="deviceActionEdit.visiable"
        :instId="this.queryParams.instId"
        :deviceActionInfoData="this.dataSource.select">
      </device-action-edit>
    </div>
  </a-card>
</template>

<script>
  import RangeDate from '@/components/datetime/RangeDate'
  import {mapState} from "vuex"
  import DeviceActionInfo from "./DeviceActionInfo";
  import DeviceActionEdit from "./DeviceActionEdit";

  export default {
    name: "DeviceAction",
    components: {DeviceActionEdit, DeviceActionInfo, RangeDate},
    data() {
      return {
        advanced: false,
        deviceActionInfo: {
          visiable: false,
          data: {}
        },
        deviceActionEdit: {
          visiable: false
        },
        queryParams: {
          searchParam: '',
          instId: ''
        },
        dataSource: [],
        sortedInfo: null,
        paginationInfo: null,
        selectedRowKeys: [],
        pagination: {
          pageSizeOptions: ['10', '20', '30', '40', '100'],
          defaultCurrent: 1,
          defaultPageSize: 10,
          showQuickJumper: true,
          showSizeChanger: true,
          showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
        },
        loading: false,
        instIdData: [],
        da: []
      }
    },
    computed: {
      ...mapState({
        user: state => state.account.user,

      }),
      columns() {
        let {sortedInfo} = this
        sortedInfo = sortedInfo || {}
        let {filteredInfo} = this
        filteredInfo = filteredInfo || {}
        return [{
          title: '设备编号',
          dataIndex: 'deviceNo',
          scopedSlots: {customRender: 'deviceNo'},
        }, {
          title: '终端型号',
          dataIndex: 'modelName',
          scopedSlots: {customRender: 'modelName'},

        }, {
          title: '终端厂商',
          dataIndex: 'brandName',
          scopedSlots: {customRender: 'brandName'},

        }, {
          title: 'S/N号',
          dataIndex: 'sn',
          scopedSlots: {customRender: 'sn'},

        }, {
          title: '处理类型',
          dataIndex: 'action',
          customRender: (text, row, index) => {
            switch (text) {
              case '40':
                return <a-tag color = "cyan" > 装机出库 < /a-tag>
              case '41':
                return <a-tag color = "pink" > 已安装(租赁) < /a-tag>
              case '42':
                return <a-tag color = "pink" > 返还入库 < /a-tag>
              case '30':
                return <a-tag color = "pink" > 维修出库 < /a-tag>
              case '31':
                return <a-tag color = "cyan" > 修复入库 < /a-tag>
              case '51':
                return <a-tag color = "pink" > 撤机 < /a-tag>
              case '52':
                return <a-tag color = "pink" > 撤机入库(终止租赁) < /a-tag>
              default:
                return text
            }
          },
          filters: [
            {text: '装机出库', value: '40'},
            {text: '已安装(租赁)', value: '41'},
            {text: '返还入库', value: '42'},
            {text: '维修出库', value: '30'},
            {text: '修复入库', value: '31'},
            {text: '撤机', value: '51'},
            {text: '撤机入库(终止租赁)', value: '52'}
          ],
          filterMultiple: false,
          filteredValue: filteredInfo.action || null,
          onFilter: (value, record) => record.action.includes(value)
        }, {
          title: '租赁商户编码',
          dataIndex: 'mchntNo',
          scopedSlots: {customRender: 'mchntNo'},

        }, {
          title: '租赁商户名称',
          dataIndex: 'mchntName',
          scopedSlots: {customRender: 'mchntName'},

        }, {
          title: '租赁起始日期',
          dataIndex: 'leaseStartDate',
          scopedSlots: {customRender: 'leaseStartDate'},

        }, {
          title: '租赁截止日期',
          dataIndex: 'leaseEndDate',
          scopedSlots: {customRender: 'leaseStartDate'},

        }, {
          title: '交易卡号',
          dataIndex: 'cardNo',
          scopedSlots: {customRender: 'cardNo'},

        }, {
          title: '状态',
          dataIndex: 'status',
          customRender: (text, row, index) => {
            switch (text) {
              case '00':
                return <a-tag color = "cyan" > 正常 < /a-tag>
              case '01':
                return <a-tag color = "pink" > 注销 < /a-tag>

              default:
                return text
            }
          },
          filters: [
            {text: '正常', value: '00'},
            {text: '注销', value: '01'},
          ],
          filterMultiple: false,
          filteredValue: filteredInfo.status || null,
          onFilter: (value, record) => record.status.includes(value)
        }, {
          title: '登记操作人',
          dataIndex: 'oprId',
          scopedSlots: {customRender: 'oprId'},

        }, {
          title: '登记时间',
          dataIndex: 'createTime',
          scopedSlots: {customRender: 'createTime'},

        }, {
          title: '法人行',
          dataIndex: 'instId',
          scopedSlots: {customRender: 'instId'},

        }, {
          title: '终端品牌操作',
          dataIndex: 'brandOperation',
          fixed: 'right',
          scopedSlots: {customRender: 'brandOperation'}
        }]
      }
    },
    mounted() {
      this.$get('admin/upmporg/getInstList').then((r) => {
        this.instIdData = r.data
      })
    },
    methods: {
      onSelectChange(selectedRowKeys) {
        this.selectedRowKeys = selectedRowKeys
      },

      handleInstChange(value) {
        this.queryParams.instId = value;
        if (value != "") {
          let params = {}
          params.instId = value;
          this.$get('deviceAction/', {...params}).then((r) => {
            this.dataSource = r.data.rows;
            this.search();
          })
        }
      },

      viewDeviceAction(record) {
        this.$refs.deviceActionInfo.setDeviceActionInfoFormValues(record)
        this.deviceActionInfo.visiable = true
      },
      handleDeviceActionInfoClose() {
        this.deviceActionInfo.visiable = false
      },
      editDeviceAction(record) {
        this.$refs.deviceActionEdit.setDeviceActionInfoFormValues(record)
        this.deviceActionEdit.visiable = true

      },

      handleDeviceActionEditSuccess() {
        this.deviceActionEdit.visiable = false
        this.$message.success('修改成功')
        this.search()
      },
      handleDeviceActionEditClose() {
        this.deviceActionEdit.visiable = false
      },

      search() {
        this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
        if (this.paginationInfo) {
          this.paginationInfo.current = this.pagination.defaultCurrent
          this.paginationInfo.pageSize = this.pagination.defaultPageSize
        }

        let {sortedInfo, filteredInfo} = this
        let sortField, sortOrder
        // 获取当前列的排序和列的过滤规则
        if (sortedInfo) {
          sortField = sortedInfo.field
          sortOrder = sortedInfo.order
        }
        let params = {}
        params.instId = this.queryParams.instId
        params.deviceNo = this.queryParams.searchParam
        params.brandName = this.queryParams.searchParam
        params.modelName = this.queryParams.searchParam

        this.fetch({
          sortField: sortField,
          sortOrder: sortOrder,
          ...params,
          ...filteredInfo
        })
      },
      reset() {
        // 取消选中
        this.selectedRowKeys = []
        // 重置分页
        this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
        if (this.paginationInfo) {
          this.paginationInfo.current = this.pagination.defaultCurrent
          this.paginationInfo.pageSize = this.pagination.defaultPageSize
        }
        // 重置列排序规则
        this.sortedInfo = null
        // 重置查询参数
        this.queryParams = {}
        // // 清空时间选择
        // this.$refs.createTime.reset()
        this.fetch()
      },
      handleTableChange(pagination, filters, sorter) {
        this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
        if (this.paginationInfo) {
          this.paginationInfo.current = this.pagination.defaultCurrent
          this.paginationInfo.pageSize = this.pagination.defaultPageSize
        }

        // 将这三个参数赋值给Vue data，用于后续使用
        this.paginationInfo = pagination
        this.filteredInfo = filters
        this.sortedInfo = sorter
        // this.userInfo.visiable = false

        this.fetch({
          sortField: sorter.field,
          sortOrder: sorter.order,
          ...this.queryParams,
          ...filters
        })
      },
      fetch(params = {}) {
        this.loading = true
        if (this.paginationInfo) {
          // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
          this.$refs.TableInfo.pagination.current = this.paginationInfo.current
          this.$refs.TableInfo.pagination.pageSize = this.paginationInfo.pageSize
          params.pageSize = this.paginationInfo.pageSize
          params.pageNum = this.paginationInfo.current
        } else {
          // 如果分页信息为空，则设置为默认值
          params.pageSize = this.pagination.defaultPageSize
          params.pageNum = this.pagination.defaultCurrent
        }
        this.$get('deviceAction', {
          ...params
        }).then((r) => {
          let data = r.data
          const pagination = {...this.pagination}
          pagination.total = data.total
          this.dataSource = data.rows
          this.pagination = pagination
          this.loading = false
        })
      }
    }
  }
</script>

<style lang="less" scoped>
  @import "../../../../../../../static/less/Common";
</style>

